package xuhaijia.lanou3g.autohome.base;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

/**
 * Created by 7 on 16/8/31.
 */
public class MyImageLoader implements ImageLoader.ImageListener {

    public Bitmap mBitmap;
    private ImageView view;
    private int defaultImageResId;
    private int errorImageResId;

    public MyImageLoader(int defaultImageResId, int errorImageResId, ImageView view) {
        this.defaultImageResId = defaultImageResId;
        this.errorImageResId = errorImageResId;
        this.view = view;
    }

    public void setBitmap(Bitmap bitmap) {
        mBitmap = bitmap;
    }

    public Bitmap getmBitmap() {
        return mBitmap;
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        if (errorImageResId != 0) {
            view.setImageResource(errorImageResId);
        }
    }

    @Override
    public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
        if (response.getBitmap() != null) {
            view.setImageBitmap(response.getBitmap());
            setBitmap(response.getBitmap());
        } else if (defaultImageResId != 0) {
            view.setImageResource(defaultImageResId);
        }
    }
}
